<template>
  <div class="type-nav">
    <div class="container">
      <!--事件委派  over|out   enter|leave  唯一没有冒泡的事件-->
      <div @mouseleave="leaveHandler">
        <h2 class="all" @mouseenter="changeViste" >
          全部商品分类
        </h2>
        <!--商品分类:一级、二级、三级地方-->
        <transition name="sort">
        <div class="sort" v-show="isViste">
          <div class="all-sort-list2" @click="goSeach">
            <!--item类名代表一个一级分类-->
            <div
              class="item"
              v-for="(c1, index) in categoryList"
              :key="c1.categoryId"
            >
              <!--mouseover|mouseenter:区别?over冒泡的 enter不冒泡-->
              <h3
                :class="{ active: index == currentIndex }"
                @mouseenter="enterHandler(index)"
              >
                <a
                  :data-categoryName="c1.categoryName"
                  :data-category1Id="c1.categoryId"
                  >{{ c1.categoryName }}</a
                >
              </h3>
              <div
                class="item-list clearfix"
                :style="{ display: currentIndex == index ? 'block' : 'none' }"
              >
                <div class="subitem">
                  <!--dl:代表一个二级分类-->
                  <dl
                    class="fore"
                    v-for="(c2, index) in c1.categoryChild"
                    :key="c2.categoryId"
                  >
                    <dt>
                      <a
                        :data-categoryName="c2.categoryName"
                        :data-category2Id="c2.categoryId"
                        >{{ c2.categoryName }}</a
                      >
                    </dt>
                    <dd>
                      <!--em:代表一个三级分类-->
                      <em
                        v-for="(c3, index) in c2.categoryChild"
                        :key="c3.categoryId"
                      >
                        <a
                          :data-categoryName="c3.categoryName"
                          :data-category3Id="c3.categoryId"
                          >{{ c3.categoryName }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
//通过vuex提供的辅助函数获取仓库的数据，组件展示数据
//mapState函数要么传递数组+对象
import { mapState } from "vuex";

//由于需要节流:引入lodash
//如果使用到lodash最好按需引入
import throttle from "lodash/throttle";
export default {
  name: "TypeNav",
  //定义响应式数据的地方
  data() {
    return {
      //存储鼠标进入H3元素相应索引值
      currentIndex: -1,
      //控制typeNav在Home|Search商品分类显示与隐藏
      isViste: true,
    };
  },
  //当组件挂载完毕:在组件mounted发请求
  mounted() {
    //如果进入的是搜索模块isViste属性值变为false
    if (this.$route.path != "/home") {
      this.isViste = false;
    }
  },
  //通过计算属性结合辅助函数让组件获取仓库数据
  computed: {
    ...mapState({
      //对象:属性与属性值
      //给VC计算出一个新的属性,state=>大仓库存储数据之state = {home:{},search:{},user:{}}
      categoryList: (state) => state.home.categoryList,
    }),
  },
  //定义VC实例方法地方
  methods: {
    //鼠标进入H3的事件回调
    enterHandler: throttle(function (index) {
      this.currentIndex = index;
    }, 10),
    //鼠标离开的事件回调
    leaveHandler() {
      //修改响应式数据变为-1
      this.currentIndex = -1;
      //鼠标离开隐藏，保证当前是search模块
      if (this.$route.path != "/home") {
        this.isViste = false;
      }
    },
    //事件委派点击跳转到search
    goSeach(event) {
      //如果编程式导航+事件委派存在问题有如下几个问题
      //1:由于div的子节点类型很多,在回调函数当中我怎么才能知道点击的一定是a标签！！！
      //2:如何区分一级分类[category1Id]、二级分类 [category2Id]、三级分类[category3Id]的a标签

      //先获取当前触发事件子节点|通过datset获取触发这个事件元素身上自定义属性
      //data-categoryName:通过dataset获取DOM自定义属性与属性值的时候  {categoryname:'xxx'}
      let { categoryname, category1id, category2id, category3id } =
        event.target.dataset;
      //点击一定是a标签
      if (categoryname) {
        var locations = {
          name: "search",
          query: { categoryName: categoryname },
        };
        //一级分类的a标签
        if (category1id) {
          locations.query.category1Id = category1id;
          //二级分类的a标签
        } else if (category2id) {
          locations.query.category2Id = category2id;
        } else {
          //三级分类的a标签
          locations.query.category3Id = category3id;
        }

        locations.params = this.$route.params;
        //需要在if条件语句里面书写
        this.$router.push(locations);
      }
    },
    //鼠标进入显示商品分类
    changeViste() {
      this.isViste = true;
    },
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }

            &.active {
              background: yellowgreen;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
        }
      }
    }

    /***进入阶段几个类名****/
    .sort-enter {
      height: 0px;
      transform: rotate(0deg);
    }

    .sort-enter-active {
      transition: all .5s;
    }

    .sort-enter-to {
      height: 461px;
      transform: rotate(360deg);
    }
  }
}
</style>